<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" >
				<el-form-item>
					<el-input v-model="keyword" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="queryKeyword">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="systemDictionaryTypes" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="sn" label="字典类型" width="120" sortable>
      </el-table-column>

      <el-table-column prop="name" label="名称" min-width="180" sortable>
      </el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="totals" :current-page="currentPage"  style="float:right;">
			</el-pagination>
		</el-col>




    <!-- 添加或修改的对话框
    el-dialog - elementUi提供的对话框 = 模态框
    	:title="title" - 动态改变标题
    	:visible.sync="saveFormVisible"  - 模态框的显示状态
    	:close-on-click-modal="false" - 关闭模态框只能通过模态框的x关闭，点击空白处不能关闭模态框
    el-form - 表单
    	:model="saveForm" - 绑定表单对象
    	label-width="80px" - 标签宽度
    	:rules="saveFormRules" - 验证规则
    	ref="saveForm" - 为当前模态框取名
    	prop="name" - 如果当前字段要验证，就必须写，出现*
    	auto-complete="off" - 不自动提示
-->
		<!--编辑界面-->
		<el-dialog :title="title"  :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="名字" prop="name">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="字典编号" prop="sn">
          <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
        </el-form-item>



			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>


	</section>
</template>

<script>

	export default {
		data() {
			return {
       //分页相关的数据
        systemDictionaryTypes: [],//绑定到表格，显示列表数据
        totals: 0,      //总条数
        currentPage:1,  //当前页
        pageSize: 5,    //当前页显示的数据
        listLoading:false,

        //高级查询参数
        keyword:'',


        //批量删除参数
				sels: [],//列表选中列

        //添加修改相关数据
				saveFormVisible: false,//编辑界面是否显示
				saveLoading: false,     //加载框
				title :'',              //标题
        saveFormRules: { //严重规则
					name: [ //在失去焦点的时候，校验name是否为空，如果为空，就会显示红色的请输入店铺的编号
						{ required: true, message: '输入名称', trigger: 'blur' }
					],

				},
				//编辑界面数据
				saveForm: {
          id: null,
          name: '',
          sn:'',


				},
        //字典信息
        types: [],




      }
		},
		methods: {
      //获取用户列表
     //1*分页栏方法
      getSystemDictionaryTypes() {
        let para = {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          keyword:this.keyword

        };
        this.listLoading = true;
        //NProgress.start();
        this.$http.post("/dictionarytype",para).then((res) => {
          this.totals = res.data.totals;
          this.systemDictionaryTypes = res.data.data;
          this.listLoading = false;
          //NProgress.done();
        }).catch(res =>{
          this.$message.error('系统繁忙');
        });
      },
     //2*分页查询方法-点击某一页触发的方法
      handleCurrentChange(val) {
        this.currentPage = val; //var前端的当前页
        this.getSystemDictionaryTypes();
      },
    //3*高级查询方法
      queryKeyword() {
      this.currentPage = 1; //var前端的当前页
      this.getSystemDictionaryTypes();
    },


			//4*删除方法
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {//点击了确认
					this.listLoading = true;//页面加载忙等
          this.$http.delete("/dictionarytype/"+row.id).then(result =>{
            result = result.data;
            if (result.success){
              this.$message({
                message: result.message,
                type: 'success'
              });
              //如果时最后一页并且时最后一条数据的时候：当前页 = 当前页-1
              //最后一条数据：(this.totals - 1)%this.pageSize == 0
              //总页数：Math.ceil(this.totals/this.pageSize)
              var totalPage = Math.ceil(this.totals/this.pageSize);
              if(totalPage==this.currentPage && this.currentPage>1 && (this.totals - 1)%this.pageSize == 0){
                this.currentPage = this.currentPage - 1;
              }
              //刷新
              this.getSystemDictionaryTypes()
            }else {
              this.$message({
                message: "系统繁忙稍后重试",
                type: 'error'
              });
              //业务删除后是否需要定位到第一页
              this.currentPage = 1;
              //刷新
              this.getSystemDictionaryTypes()
            }
          }).catch(result=>{
            this.listLoading = false;//页面加载忙等
            this.$message({
              message: "系统繁忙稍后重试",
              type: 'error'
            });
          })

				}).catch(() => {//点击取消按钮

				});
			},

      //5*选项改变的时候触发
      selsChange: function (sels) {
        this.sels = sels;
      },






      //6*批量删除
      batchRemove: function () {
       //获取被选中的对象id们
        var ids = this.sels.map(item => item.id)
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;//加载忙等框
          this.$http.patch("/dictionarytype",ids).then(res=>{
          this.listLoading = false
          if (res.data.success){
              this.currentPage = 1;//返回第一页
            this.getSystemDictionaryTypes()  //刷新
          }else {
            this.$message.error("批量删除失败【500】");
          }

          }).catch( res =>{
            this.$message.error("系统错误【400,404】");

          });

        }).catch(() => {

        });
      },

      //7*点击修改按钮弹出对话框
			handleEdit: function (index, row) {

        this.title = "编 辑"
				this.saveForm = Object.assign({}, row);
        //获取店长信息
        this.gettypes();
        //显示对话框
        this.saveFormVisible = true;
			},
			//8*点击添加按钮弹出对话框
			handleAdd: function () {

        this.title = "添 加"
				this.saveForm = {
          id: null,
          name: '',
          sn:'',
				};

        //获取店长的值添加的时候
        this.gettypes();

        this.saveFormVisible = true;
			},
			//9*点击模态框提交数据
      saveSubmit: function () {
        //所有对话框中  一个名称为 saveForm的对话框  验证【上面的验证规则通过了 - 返回true，否则返回false】
        this.$refs.saveForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.saveLoading = true;
              let para = Object.assign({}, this.saveForm);

              this.$http.put("/dictionarytype",para).then(res => {
                if(res.data.success){
                  this.$message.success("操作成功");
                  this.getSystemDictionaryTypes();
                }else{
                  this.$message.error("系统繁忙，请稍后重试")
                }
                this.saveLoading = false;
                this.saveFormVisible = false;
              }).catch(res => {
                this.$message.error("系统繁忙，请稍后重试")
              })
            });
          }
        });
			},


      gettypes() {
        this.$http.get("/dictionarytype").then(res => {
          this.types = res.data;//给字典类型赋值
        })
      },



    },
		mounted() {
			this.getSystemDictionaryTypes();
		}
	}

</script>

<style scoped>

</style>